/**
 * ----------------------------------------------------------------------------
 * Copyright Jens Konopka
 * 
 * Alle Rechte vorbehalten.
 * Licence: Apache 2.0
 * ----------------------------------------------------------------------------
 *
 * created on 05.03.2012 by jkonopka
 */
package de.sql.toolkit.generator;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.sql.toolkit.model.Table;
import de.sql.toolkit.util.StringUtil;

/**
 * @author jkonopka
 * 
 */
public class JvModelGenerator extends AbstractJvModelGenerator {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3446807468425518472L;
	private List<AbstractJvModelGenerator> writer = new ArrayList<AbstractJvModelGenerator>();
	private String namespace;

	/**
	 * @param dstPath
	 * @param table
	 * @param namespace
	 */
	public JvModelGenerator(File dstPath, Table table, final String namespace) {
		super(dstPath, table);
		this.namespace = namespace;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.sql.toolkit.writer.IWriter#write()
	 */
	@Override
	public String generate() throws Exception {
		final StringBuilder content = new StringBuilder();
		content.append("package ");
		content.append(getNamespace());
		content.append(";\n\n");

		final AbstractJvModelGenerator ajmWriter = new JvModelImportsGenerator(getDstPath(), getTable());
		content.append(ajmWriter.generate());
		content.append("public class ");
		content.append(StringUtil.upperCaseFirstLetters(getTable().getName()));
		content.append(" implements Serializable {\n\n");
		content.append("\tprivate static final long serialVersionUID = -1L;\n");

		for (final AbstractJvModelGenerator writer : this.writer) {
			content.append(writer.generate());
		}

		content.append("}\n");
		return content.toString();
	}

	/**
	 * @param writer
	 */
	public void add(final AbstractJvModelGenerator writer) {
		this.writer.add(writer);
	}

	/**
	 * @return the writer
	 */
	protected final List<AbstractJvModelGenerator> getWriter() {
		return writer;
	}

	/**
	 * @param writer the writer to set
	 */
	protected final void setWriter(List<AbstractJvModelGenerator> writer) {
		this.writer = writer;
	}

	/**
	 * @return the namespace
	 */
	protected final String getNamespace() {
		return namespace;
	}

	/**
	 * @param namespace the namespace to set
	 */
	protected final void setNamespace(String namespace) {
		this.namespace = namespace;
	}

}
